﻿package UIComponents
{
	
	import Classes.staticinfo.Language;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/** 
	 * @author thy
	 * @version 1.0.0 
	 * 创建时间：Jun 28, 2012 11:24:57 AM 
	 */ 
	public class PorgramNpcDialogue extends Sprite
	{
		private var npcContext:TextField;
		private var npcIcon:IconContainer;
		private var npcBtn:DisplayObject;
		private var npcTiltle:IconContainer;
		private var programTxt:TextField;
		private var programBar:MovieClip;
		
		public function PorgramNpcDialogue()
		{
			init();
		}
		
		private function init():void
		{
			this.npcContext=this.getChildByName("npcContext") as TextField;	
			this.npcContext.mouseEnabled=false;
			
			this.npcTiltle=this.getChildByName("titleIc") as IconContainer;
			
			this.npcIcon=this.getChildByName("NPCIcon") as IconContainer;
			
			this.npcBtn=this.getChildByName("closeBtn");
			
			this.programTxt=this.getChildByName("programTxt") as TextField;
			
			this.programBar=this.getChildByName("programBar") as MovieClip; 
			
			initAddListener();
		}
		
		private function initAddListener():void
		{
			this.npcBtn.addEventListener(MouseEvent.CLICK,clickFun);
		}
		
		
		private function clickFun(e:MouseEvent):void
		{
			this.dispatchEvent(new MouseEvent("closeNpcDialogue"));
			resetProgram();
		}
		
		/**
		 * 设置基本属性 
		 * @param title
		 * @param context
		 * @param contextPic
		 * 
		 */		
		public function setPorgramNpcDialogueInfo(title:DisplayObject=null,context:String="",contextPic:DisplayObject=null,jindu:int=0):void
		{
			this.reset();
			this.npcTiltle.addPic(title);
			this.npcContext.htmlText=context;
			this.npcIcon.addPic(contextPic);
//			if(jindu>0)this.controlProgramBar(jindu);
		}
		
		private var value:int;
		public function controlProgramBar(value:int):void
		{
//			this.resetProgram();
			this.value=value;
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
//			this.programBar.play();
//			this.programBar.gotoAndStop(value);
//			this.programTxt.text=value+"%";
		}
				
		private function enterFrame(e:Event):void
		{
			if(this.programBar.currentFrame>this.value || (this.programBar.currentFrame==100 && this.value==100))
			{
				this.programBar.stop();
				this.removeEventListener(Event.ENTER_FRAME,enterFrame);
				if(this.programBar.currentFrame==100 && this.value==100)
				{
					this.programTxt.text=Language.programValue+this.programBar.currentFrame+"%";
				}
				
			}else{
				this.programBar.play();
				this.programTxt.text=Language.programValue+this.programBar.currentFrame+"%";
//				trace("this.programBar.currentFrame="+this.programBar.currentFrame +" this.programTxt.text= "+this.programTxt.text);
			}
			
		}
				
			
		private function reset():void
		{
			this.npcTiltle.addPic(null);
			this.npcContext.text="";
			this.npcContext.htmlText="";
			this.programTxt.text="";
			this.npcIcon.addPic(null);
			this.resetProgram();
		}
		
		private function resetProgram():void
		{
			this.value=1;
			this.programBar.gotoAndStop(1);
//			this.programBar.currentFrame=1;
		}
	}
}
